﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XCoder
{
    /// <summary>
    /// 字段信息
    /// </summary>
    public class ColumnInfo
    {
        /// <summary>
        /// 获取绑定的表
        /// </summary>
        public TableInfo Table { get; private set; }

        /// <summary>
        /// 获取字段名称
        /// </summary>
        public string ColumnName { get; private set; }

        /// <summary>
        /// 获取列在表中对应的位置
        /// </summary>
        public int Ordinal { get; set; }

        /// <summary>
        /// 获取字段备注
        /// </summary>
        public string Comment { get; set; }

        /// <summary>
        /// 是否为主键
        /// </summary>
        public bool IsPrimaryKey { get; set; }

        /// <summary>
        /// 获取或设置列是否允许空值。
        /// </summary>
        public bool AllowDBNull { get; set; }

        /// <summary>
        /// 获取或设置列是否唯一。
        /// </summary>
        public bool Unique { get; set; }

        /// <summary>
        /// 获取或设置列是否将列的值自动递增。
        /// </summary>
        public bool AutoIncrement { get; set; }

        /// <summary>
        /// 获取或设置数据库类型
        /// </summary>
        public string DataType { get; set; }

        /// <summary>
        /// 获取或设置默认值
        /// </summary>
        public object DefaultValue { get; set; }

        /// <summary>
        /// 最大长度
        /// </summary>
        public int MaxLength { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <param name="columnName"></param>
        /// <param name="dataType"></param>
        /// <param name="comment"></param>
        public ColumnInfo(TableInfo table, string columnName, string comment = null)
        {
            this.Table = table;
            this.ColumnName = columnName;
            this.Comment = comment;
        }

        /// <summary>
        /// 获取代码类型
        /// </summary>
        /// <returns></returns>
        public string GetCodeType()
        {
            string codeTypeStr = this.DataType;
            Type codeType = null;
            try
            {
                codeType = Type.GetType(codeTypeStr);
            }
            catch { }
            if (codeType != null && codeType.IsValueType && this.Table.Database.AllowTypeNullable)
            {
                codeTypeStr = codeTypeStr + "?";
            }
            return codeTypeStr;
        }

        /// <summary>
        /// 转换成属性代码
        /// </summary>
        /// <returns></returns>
        public string ToModelCode()
        {
            StringBuilder modelCode = new StringBuilder();

            string codeTypeStr = this.GetCodeType();

            #region 添加私有字段

            //   添加私有字段
            modelCode.AppendLine(string.Concat("        private ", codeTypeStr, " _", this.ColumnName, ";"));

            #endregion

            #region 添加注释

            //  添加注释
            modelCode.AppendLine("        /// <summary>");
            foreach (string item in (this.Comment ?? string.Empty).Split(new string[] { Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries))
            {
                modelCode.AppendLine(string.Format("        /// {0}", item));
            }
            modelCode.AppendLine("        /// </summary>");

            #endregion

            #region 添加属性

            //  添加属性
            modelCode.AppendLine(string.Format("        public {0} {1}", codeTypeStr, this.ColumnName));
            modelCode.AppendLine("        {");
            modelCode.AppendLine(string.Concat("            get { return this._", this.ColumnName, "; }"));
            modelCode.AppendLine(string.Concat("            set { this._", this.ColumnName, " = value; }"));
            modelCode.AppendLine("        }");

            #endregion

            return modelCode.ToString();
        }

    }
}
